<?php

namespace App\Console\Commands\OpenShop\Contract;

use App\Console\Commands\Traits\ExcelCommandTrait;
use App\Models\OpenShop\InvestmentPromotion\OpenShopContractTemplateModel;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

class InitContractTemplateCommand extends Command
{
    use ExcelCommandTrait;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'contractTemplate:init-data';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '初始化加盟政策数据';


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     * @throws GuzzleException
     */
    public function handle()
    {
        $url = 'https://file.51kuafu.com/77/4f/571eda95abb950a6f8ec2dab187a5cc3c9506296eefdc7963f28b3f15c38.csv';

        $this->setInputExcel(false)->excelImport(InitContractTemplateCommand::class, $url);
    }

    private array $dealData = [];

    public function collection(Collection $collection, ?Command $command)
    {
        $collection->first()->map(function ($item, $row) use ($command) {
            $row = $row + 1;
            if ($row == 1) {
                return null;
            }

            // 有效期处理
            $start_time = date("Y-m-d 00:00:00");
            if ($item[12] == '永久') {
                $end_time = date("Y-m-d H:i:s", strtotime("+100 year", strtotime($start_time)));
            } else {
                $end_time = date("Y-m-d H:i:s", strtotime(str_replace('.', '-', $item[12])));
            }

            // 名称特殊处理 : 包含海外和分账的 关闭
            $isShow = OpenShopContractTemplateModel::IS_SHOW_1;
            if (strpos(strtolower($item[1]), '海外') !== false || strpos(strtolower($item[1]), '分账') !== false) {
                $isShow = OpenShopContractTemplateModel::IS_SHOW_3;
            }

            $this->dealData[] = [
                'name'                    => $item[1],
                'year'                    => $item[3],
                // 加盟费
                'initialFee'              => $item[4],
                // 技术转让费
                'technologyTransferFee'   => $item[5],
                // 保证金
                'deposit'                 => $item[6],
                // 特权使用费第一年
                'managementCost'          => $item[7],
                // 特权使用费每年
                'managementCostEveryYear' => $item[8],
                // 装修保证金
                'decorationDeposit'       => $item[9],
                // 品牌指导费
                'guidanceFee'             => $item[10],
                // 店铺数量
                'shopCount'               => $item[11],
                // 有效期
                'startTime'               => $start_time,
                'endTime'                 => $end_time,
                'isShow'                  => $isShow
            ];

        })->filter();


        if (!empty($this->dealData)) {
            foreach ($this->dealData as $k => $item) {
                $templateData = OpenShopContractTemplateModel::query()->where('templateName', $item['name'])->first();
                if (!empty($templateData)) {
//                    $templateData->initialFee              = $item['initialFee'] * 100;
//                    $templateData->technologyTransferFee   = $item['technologyTransferFee'] * 100;
//                    $templateData->deposit                 = $item['deposit'] * 100;
//                    $templateData->managementCost          = $item['managementCost'] * 100;
//                    $templateData->managementCostEveryYear = $item['managementCostEveryYear'] * 100;
//                    $templateData->decorationDeposit       = $item['decorationDeposit'] * 100;
                    $templateData->startTime               = strtotime($item['startTime']);
                    $templateData->endTime                 = strtotime($item['endTime']);
                    $templateData->isShow                  = $item['isShow'];
                    $templateData->save();

                    echo $item['name'] . " is done..." . PHP_EOL;
                }else{
                    dump($item['name'] . "--404...." .PHP_EOL);
                }

            }
        }

    }
}
